﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using System.Windows;
using MP3Ganizer.Common.Interfaces;

namespace MP3Ganizer.Common.BaseObject
{
    public abstract class BaseModule<T> : IModule where T : IModuleController
    {
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;

        public BaseModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        protected IRegionManager RegionManager
        {
            get { return _regionManager; }
        }

        protected IUnityContainer Container
        {
            get { return _container; }
        }

        public virtual void Initialize()
        {
            RegisterResources();
            RegisterViewsAndServices();
            var controller = Container.Resolve<T>();
            controller.Run();
        }

        abstract protected void RegisterViewsAndServices();

        protected virtual void RegisterResources()
        {
            try
            {
                ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri(GetType().Assembly.FullName + ";component/Module.xaml", UriKind.Relative)) as ResourceDictionary;
                if (resourceDictionary != null)
                    Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
            catch
            {
                //Logging il n'y a pas de fichier ressource
            }
        }
    }
}
